/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.client.goods.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import mscs.data.IFileDataStore;
import mscs.data.IGoodsProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.OutGoods;
import mscs.data.pojo.Person;

/**
 *
 * @author Usbuild
 */
public class GoodsUtil implements IAddGoodsUtil,IOutGoodsUtil,ISearchGoodsUtil {
    private ArrayList<Goods> goodsList = new ArrayList<Goods>();
    private ArrayList<OutGoods> outList = new ArrayList<OutGoods>();
    private IFileDataStore fds = new IGoodsProxy();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private ArrayList<Goods> allGoods;
    private Person person;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    public GoodsUtil() {
        allGoods = (ArrayList<Goods>)fds.getObjects();
    }
    public List toStringArray(Goods goods) {
        Vector<String> list = new Vector<String>();
        if(goods != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            list.add(goods.getId());
            list.add(((Integer)goods.getNum()).toString());
            list.add(goods.getDesc());
            list.add(((Double)goods.getSalPrice()).toString());
            list.add(sdf.format(goods.getProDate()));
            list.add(sdf.format(goods.getDesDate()));
            list.add(sdf.format(goods.getEntDate()));
            list.add(((Double)goods.getBasPrice()).toString());
        }
        return list;
    } 
    public Goods queryByExample(Goods obj) {
        for(Goods goods : allGoods)
            if(goods.getId().equals(obj.getId()))
                return goods;
        return null;
    }
    public List find(Goods obj) {
        Vector<Goods> reList = new Vector<Goods>();
        for(Goods goods : goodsList)
            if (goods.getId().equals(obj.getId()))
                reList.add(goods);
        return reList;
    }

    public Vector<Vector<String>> toDataTable(List list) {
    	try {
            Vector<Goods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Goods goods : seList)
            {
                reList.add((Vector<String>)toStringArray(goods));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }
    public boolean addToCache(Goods goods) {
        if (find(goods).isEmpty()) {
            this.goodsList.add(goods);
            return true;
        } else {
            return false;
        }
    }
    public ArrayList<Goods> getCacheList() {
        return goodsList;
    }
    public ArrayList<OutGoods> getOutGoodsList() {
        return outList;
    }
    public void setCacheList(ArrayList<Goods> goodsList) {
        this.goodsList = goodsList;
    }
    public boolean commitCacheList() {
        if (!goodsList.isEmpty()) {
                for(Goods goods : allGoods) {
                    for(Goods goodsCache : goodsList) {
                        if(goods.getId().equals(goodsCache.getId())) {
                            goods.setNum(goods.getNum() + goodsCache.getNum());
                            goods.setBasPrice(goodsCache.getBasPrice());
                            goods.setProDate(goodsCache.getProDate());
                            goods.setDesDate(goodsCache.getDesDate());
                        }
                    }
                }
               fds.update(allGoods);
               goodsList.clear();
               return true;
        }
        return false;
    }
    public ArrayList<Goods> queryByCriteria(String keywords, String key) {
        return (ArrayList<Goods>)fds.searchObjectsByAttr(key, ".*" + keywords + ".*");
    }
    
    public ArrayList<Goods> queryByRegExp(String key, String regexp) {
        return (ArrayList<Goods>)fds.searchObjectsByAttr(key, regexp);
    }
    public Vector searchTableVector(String keywords, String key) {
        return new Vector(toDataTable(fds.searchObjectsByAttr(key, ".*" + keywords + ".*")));
    }
    public Vector listTableVector() {
        return new Vector(toDataTable(fds.getObjects()));
    }
    public void deleteByIndex(int[] index) {
    	fds.deleteByIndex(index);
    }
    //传入数量、描述以及索�?    
	public boolean addToOutList(int num, String reason, int[] index) {
        Goods goods = allGoods.get(index[0]);
        if(goods.getNum() >= num) {
            OutGoods outGoods = new OutGoods();
            outGoods.setGoods(goods);
            outGoods.setNum(num);
            outGoods.setReason(reason);
            goods.setNum(goods.getNum() - num);
            outList.add(outGoods);
            indexes.add(index[0]);
            return true;
        } else {
            return false;
        }
    }
    public void removeFormOutList(int[] index) {
        int oldTable = indexes.get(index[0]);
        allGoods.get(oldTable).setNum(allGoods.get(oldTable).getNum() + outList.get(index[0]).getNum());
        outList.remove(index[0]);
        indexes.remove(index[0]);
    }
    public List outToStringArray(OutGoods outGoods) {
        Vector<String> list = new Vector<String>();
        list.add(outGoods.getId());
        list.add(outGoods.getDesc());
        list.add(((Integer)outGoods.getNum()).toString());
        list.add(outGoods.getReason());
        return list;
    }
    public Vector outTableVector() {
        return new Vector(outToDataTable(outList));
    }
    
    public Vector<Vector<String>> outToDataTable(List list) {
    	try {
            Vector<OutGoods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (OutGoods goods : seList)
            {
                reList.add((Vector<String>)outToStringArray(goods));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }
    public Vector<Vector<String>> refreshTableVector() {
       return toDataTable(allGoods);
    }
    public void endOut() {
        outList.clear();
        fds.update(allGoods);
    }

    public Vector<Vector<String>> addListVector() {
        return toDataTable(getCacheList());
    }
    
    public void removeFromCache(int index) {
        goodsList.remove(index);
    }
    public Goods getCacheByIndex(int index) {
        return goodsList.get(index);
    }
    public void setCacheByIndex(int index, Goods goods) {
        goodsList.set(index, goods);
    }
}